﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Nevron.Chart;
using Nevron.GraphicsCore;
using System.Collections;
using System.IO;
using System.Configuration;

namespace IGuess.KNN
{
    public partial class KNNForm : Form
    {

        private ClassifierForm form;
        private KNNClassifier classifier;
        private string fileName; 
        private NPointSeries m_Point;


        public KNNForm(ClassifierForm form, KNNClassifier classifier, string fileName)
        {
            InitializeComponent();
            this.form = form;
            this.classifier = classifier;
            this.fileName = fileName;
        }

        private void loadResources()
        {

            this.KNNFormAcceptButton.Text = RM.GetString("KNNFormAcceptButton");
            this.KNNFormResultLabel.Text = RM.GetString("KNNFormResultLabel");
            this.KNNFormResultTextLabel.Text = this.classifier.Classify();
            this.KNNFormResumeGroupBox.Text = RM.GetString("KNNFormResumeGroupBox");
            this.KNNFormStatsGroupBox.Text = RM.GetString("KNNFormStatsGroupBox");

        }

        private void KNNForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.form.Enabled = true;
        }

        private void KNNFormAcceptButton_Click(object sender, EventArgs e)
        {
            this.form.Enabled = true;
            this.Close();
        }

        private void LoadStats()
        {

            // configure the chart
            NChart chart = KNNFormStatsNChartControl.Charts[0];

            // setup chart
            chart = KNNFormStatsNChartControl.Charts[0];
            chart.Axis(StandardAxis.Depth).Visible = false;

            // add interlace stripe
            NLinearScaleConfigurator linearScale = chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator as NLinearScaleConfigurator;
            NScaleStripStyle stripStyle = new NScaleStripStyle(new NColorFillStyle(Color.Beige), null, true, 0, 0, 1, 1);
            stripStyle.Interlaced = true;
            stripStyle.SetShowAtWall(ChartWallType.Back, true);
            stripStyle.SetShowAtWall(ChartWallType.Left, true);
            linearScale.StripStyles.Add(stripStyle);

            // setup point series
            m_Point = (NPointSeries)chart.Series.Add(SeriesType.Point);
            m_Point.Name = "Point Series";
            m_Point.InflateMargins = true;
            m_Point.ShadowStyle.Type = ShadowType.GaussianBlur;
            m_Point.ShadowStyle.Offset = new NPointL(2, 2);
            m_Point.ShadowStyle.Color = Color.FromArgb(80, 0, 0, 0);
            m_Point.ShadowStyle.FadeLength = new NLength(5);
            m_Point.DataLabelStyle.Visible = true;

            foreach (DictionaryEntry Item in this.classifier.EuclideanDistances)
            {

                m_Point.AddDataPoint(new NDataPoint(Math.Round(Convert.ToDouble(Item.Value),3), Item.Key.ToString()));

            }

            m_Point.AddDataPoint(new NDataPoint(Math.Round(this.classifier.Mean), "TextAnalized"));

            m_Point.Legend.Mode = SeriesLegendMode.DataPoints;

            NStyleSheet styleSheet = NStyleSheet.CreatePredefinedStyleSheet(PredefinedStyleSheet.NevronMultiColor);
            styleSheet.Apply(KNNFormStatsNChartControl.Document);

        }

        public void writeStats()
        {
            string[,] statsFile = new string[3, 4];

            if (File.Exists(ConfigurationManager.AppSettings["StatisticsFile"]))
            {

                //Read the file and store its content
                using (System.IO.StreamReader sr = new System.IO.StreamReader(ConfigurationManager.AppSettings["StatisticsFile"]))
                {
                    string line;
                    int i = 0;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] token = line.Split();
                        statsFile[i, 0] = token[0];
                        statsFile[i, 1] = token[1];
                        statsFile[i, 2] = token[2];
                        statsFile[i, 3] = token[3];
                        i++;
                    }

                }

                if (this.fileName.ToLower().Contains("en") && classifier.Result.ToLower().Equals("en"))
                {

                    int totalENTexts = Convert.ToInt32(statsFile[0, 1]);
                    int CorrectENTexts = Convert.ToInt32(statsFile[0, 2]);
                    int totalAnalizedTexts = Convert.ToInt32(statsFile[0, 3]);

                    statsFile[0, 1] = (totalENTexts + 1).ToString();
                    statsFile[0, 2] = (CorrectENTexts + 1).ToString();
                    statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("de") && classifier.Result.ToLower().Equals("de"))
                {

                    int totalENTexts = Convert.ToInt32(statsFile[1, 1]);
                    int CorrectENTexts = Convert.ToInt32(statsFile[1, 2]);
                    int totalAnalizedTexts = Convert.ToInt32(statsFile[1, 3]);

                    statsFile[1, 1] = (totalENTexts + 1).ToString();
                    statsFile[1, 2] = (CorrectENTexts + 1).ToString();
                    statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("fr") && classifier.Result.ToLower().Equals("fr"))
                {

                    int totalENTexts = Convert.ToInt32(statsFile[2, 1]);
                    int CorrectENTexts = Convert.ToInt32(statsFile[2, 2]);
                    int totalAnalizedTexts = Convert.ToInt32(statsFile[2, 3]);

                    statsFile[2, 1] = (totalENTexts + 1).ToString();
                    statsFile[2, 2] = (CorrectENTexts + 1).ToString();
                    statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else
                {

                    if (this.fileName.ToLower().Contains("en"))
                    {

                        int totalENTexts = Convert.ToInt32(statsFile[0, 1]);
                        int totalAnalizedTexts = Convert.ToInt32(statsFile[0, 3]);

                        statsFile[0, 1] = (totalENTexts + 1).ToString();
                        statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                            statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                            statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("de"))
                    {

                        int totalDETexts = Convert.ToInt32(statsFile[0, 1]);
                        int totalAnalizedTexts = Convert.ToInt32(statsFile[1, 3]);

                        statsFile[1, 1] = (totalDETexts + 1).ToString();
                        statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                            statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                            statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("fr"))
                    {

                        int totalENTexts = Convert.ToInt32(statsFile[2, 1]);
                        int totalAnalizedTexts = Convert.ToInt32(statsFile[2, 3]);

                        statsFile[2, 1] = (totalENTexts + 1).ToString();
                        statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                            statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                            statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }

                }


            }
            else
            {

                if (this.fileName.ToLower().Contains("en") && classifier.Result.ToLower().Equals("en"))
                {

                    statsFile[0, 0] = "EN";
                    statsFile[0, 1] = 1.ToString();
                    statsFile[0, 2] = 1.ToString();
                    statsFile[0, 3] = 1.ToString();
                    statsFile[1, 0] = "DE";
                    statsFile[1, 1] = 0.ToString();
                    statsFile[1, 2] = 0.ToString();
                    statsFile[1, 3] = 1.ToString();
                    statsFile[2, 0] = "FR";
                    statsFile[2, 1] = 0.ToString();
                    statsFile[2, 2] = 0.ToString();
                    statsFile[2, 3] = 1.ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("de") && classifier.Result.ToLower().Equals("de"))
                {


                    statsFile[0, 0] = "EN";
                    statsFile[0, 1] = 0.ToString();
                    statsFile[0, 2] = 0.ToString();
                    statsFile[0, 3] = 1.ToString();
                    statsFile[1, 0] = "DE";
                    statsFile[1, 1] = 1.ToString();
                    statsFile[1, 2] = 1.ToString();
                    statsFile[1, 3] = 1.ToString();
                    statsFile[2, 0] = "FR";
                    statsFile[2, 1] = 0.ToString();
                    statsFile[2, 2] = 0.ToString();
                    statsFile[2, 3] = 1.ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("fr") && classifier.Result.ToLower().Equals("fr"))
                {


                    statsFile[0, 0] = "EN";
                    statsFile[0, 1] = 0.ToString();
                    statsFile[0, 2] = 0.ToString();
                    statsFile[0, 3] = 1.ToString();
                    statsFile[1, 0] = "DE";
                    statsFile[1, 1] = 0.ToString();
                    statsFile[1, 2] = 0.ToString();
                    statsFile[1, 3] = 1.ToString();
                    statsFile[2, 0] = "FR";
                    statsFile[2, 1] = 1.ToString();
                    statsFile[2, 2] = 1.ToString();
                    statsFile[2, 3] = 1.ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else
                {

                    if (this.fileName.ToLower().Contains("en"))
                    {


                        statsFile[0, 0] = "EN";
                        statsFile[0, 1] = 1.ToString();
                        statsFile[0, 2] = 0.ToString();
                        statsFile[0, 3] = 1.ToString();
                        statsFile[1, 0] = "DE";
                        statsFile[1, 1] = 0.ToString();
                        statsFile[1, 2] = 0.ToString();
                        statsFile[1, 3] = 1.ToString();
                        statsFile[2, 0] = "FR";
                        statsFile[2, 1] = 0.ToString();
                        statsFile[2, 2] = 0.ToString();
                        statsFile[2, 3] = 1.ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("de"))
                    {


                        statsFile[0, 0] = "EN";
                        statsFile[0, 1] = 0.ToString();
                        statsFile[0, 2] = 0.ToString();
                        statsFile[0, 3] = 1.ToString();
                        statsFile[1, 0] = "DE";
                        statsFile[1, 1] = 1.ToString();
                        statsFile[1, 2] = 0.ToString();
                        statsFile[1, 3] = 1.ToString();
                        statsFile[2, 0] = "FR";
                        statsFile[2, 1] = 0.ToString();
                        statsFile[2, 2] = 0.ToString();
                        statsFile[2, 3] = 1.ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("fr"))
                    {


                        statsFile[0, 0] = "EN";
                        statsFile[0, 1] = 0.ToString();
                        statsFile[0, 2] = 0.ToString();
                        statsFile[0, 3] = 1.ToString();
                        statsFile[1, 0] = "DE";
                        statsFile[1, 1] = 0.ToString();
                        statsFile[1, 2] = 0.ToString();
                        statsFile[1, 3] = 1.ToString();
                        statsFile[2, 0] = "FR";
                        statsFile[2, 1] = 1.ToString();
                        statsFile[2, 2] = 0.ToString();
                        statsFile[2, 3] = 1.ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }

                }

            }


        }

        private void KNNForm_Load(object sender, EventArgs e)
        {
            this.loadResources();
            this.LoadStats();
            this.writeStats();
            Bitmap flag = new Bitmap(this.KNNFormFlagPictureBox.Width, this.KNNFormFlagPictureBox.Height);
            
            if (this.classifier.Result.Equals("EN"))
            {
                flag = new Bitmap(@".\Resources\englishFlag_mini.png");
                this.KNNFormFlagPictureBox.Image = flag;
            }
            else if (this.classifier.Result.Equals("FR"))
            {
                flag = new Bitmap(@".\Resources\frenchFlag_mini.gif");
                this.KNNFormFlagPictureBox.Image = flag;
            }
            else if (this.classifier.Result.Equals("DE"))
            {
                flag = new Bitmap(@".\Resources\germanFlag_mini.JPG");
                this.KNNFormFlagPictureBox.Image = flag;
            }
        }
    }
}
